<template>
  <view class="cash-list-container">
    <!-- 顶部标题栏 -->
    <view class="header">
      <view class="back-btn" @click="goBack">
        <text class="back-arrow">←</text>
      </view>
      <text class="title">提现列表</text>
    </view>
    
    
    <!-- 提现列表 -->
    <view class="cash-list">
      <!-- 提现项 -->
      <view class="cash-item" v-for="(item, index) in cashRecords" :key="index">
        <view class="cash-header">
          <view class="cash-info">
            <text class="cash-amount">¥{{ item.money }}</text>
            <text class="cash-status" :class="getStatusClass(item.status)">{{ getStatusText(item.status) }}</text>
          </view>
          <text class="cash-time">{{ formatDate(item.createAt) }}</text>
        </view>
        
        <view class="cash-details">
          <view class="detail-row">
            <text class="detail-label">到账金额:</text>
            <text class="detail-value">¥{{ item.lastMoney }}</text>
          </view>
          <view class="detail-row">
            <text class="detail-label">手续费:</text>
            <text class="detail-value">¥{{ item.fee }}</text>
          </view>
          <view class="detail-row" v-if="item.status !== 1">
            <text class="detail-label">处理时间:</text>
            <text class="detail-value">{{ formatDate(item.handleAt) }}</text>
          </view>
        </view>
      </view>
      
      <!-- 空状态 -->
      <view class="empty-state" v-if="cashRecords.length === 0">
        <image src="/static/images/empty.png" mode="aspectFit" class="empty-image"></image>
        <text class="empty-text">暂无提现记录</text>
      </view>
    </view>
    
    
  </view>
</template>

<script>
import { get } from '../../utils/request.js'

export default {
  data() {
    return {
      cashRecords: [],
      searchKeyword: '',
      currentFilter: 'all',
      loading: false,
      page: 1,
      pageSize: 10,
      hasMore: true
    }
  },
  
  onLoad() {
    // 页面加载时获取提现列表
    this.getCashList()
  },
  
  methods: {
    // 返回上一页
    goBack() {
      uni.navigateBack()
    },
    
    // 获取提现列表
    async getCashList() {
      if (this.loading || !this.hasMore) {
        return
      }
      
      this.loading = true
      try {
        const token = uni.getStorageSync('token')
        if (!token) {
          uni.showToast({
            title: '请先登录',
            icon: 'none'
          })
          return
        }
        
        // 构建筛选参数
        let statusFilter = ''
        switch (this.currentFilter) {
          case 'processing':
            statusFilter = '1'
            break
          case 'success':
            statusFilter = '2'
            break
          case 'failed':
            statusFilter = '3'
            break
          default:
            statusFilter = ''
        }
        
        try {
          // 调用API获取提现列表
          const res = await get('/admin/cash/list', {
            page: this.page,
            pageSize: this.pageSize,
            keyword: this.searchKeyword,
            status: statusFilter
          })
          console.log(res.data)
          if (res.code === 0 && res.data) {
            const newRecords = res.data.list || []
            if (newRecords.length < this.pageSize) {
              this.hasMore = false
            }
            // 如果是第一页，直接替换数据；否则追加
            if (this.page === 1) {
              this.cashRecords = newRecords//.length > 0 ? newRecords : this.cashRecords
            } else {
              this.cashRecords = [...this.cashRecords, ...newRecords]
            }
            this.page++
          } else {
            this.hasMore = false
            // 如果是第一页且没有数据，保持模拟数据
            if (this.page === 1 && this.cashRecords.length === 0) {
              // 保持原有的模拟数据
            }
          }
        } catch (apiError) {
          console.warn('API调用失败，使用模拟数据:', apiError)
          // API调用失败时，保持原有数据不变
          if (this.page === 1) {
            // 第一页时保持模拟数据
          } else {
            // 加载更多时失败，提示用户
            uni.showToast({
              title: '加载更多失败',
              icon: 'none'
            })
          }
          this.hasMore = false
        }
      } catch (error) {
        console.error('获取提现列表失败:', error)
        uni.showToast({
          title: error.message || '获取提现列表失败',
          icon: 'none'
        })
      } finally {
        this.loading = false
      }
    },
    
    // 设置筛选条件
    setFilter(filter) {
      this.currentFilter = filter
      // 重置列表
      this.cashRecords = []
      this.page = 1
      this.hasMore = true
      // 重新获取数据
      this.getCashList()
    },
    
    // 搜索
    handleSearch() {
      // 重置列表
      this.cashRecords = []
      this.page = 1
      this.hasMore = true
      // 重新获取数据
      this.getCashList()
    },
    
    // 格式化日期
    formatDate(dateString) {
      if (!dateString) return '-'
      const date = new Date(dateString)
      const year = date.getFullYear()
      const month = String(date.getMonth() + 1).padStart(2, '0')
      const day = String(date.getDate()).padStart(2, '0')
      const hours = String(date.getHours()).padStart(2, '0')
      const minutes = String(date.getMinutes()).padStart(2, '0')
      return `${year}-${month}-${day} ${hours}:${minutes}`
    },
    
    // 获取状态文本
    getStatusText(status) {
      switch (status) {
        case 1:
          return '提现中'
        case 2:
          return '已提现'
        case 3:
          return '提现异常'
        default:
          return '未知状态'
      }
    },
    
    // 获取状态样式类
    getStatusClass(status) {
      switch (status) {
        case 1:
          return 'status-processing'
        case 2:
          return 'status-success'
        case 3:
          return 'status-failed'
        default:
          return ''
      }
    },
    
    // 上拉加载更多
    onReachBottom() {
      this.getCashList()
    }
  }
}
</script>

<style scoped>
.cash-list-container {
  background-color: #3A3052;
  min-height: 100vh;
  padding-bottom: 120rpx;
}

.header {
  background-color: #4A4062;
  padding: 10px 65px 10px 15px;
  display: flex;
  align-items: center;
  position: sticky;
  top: 0;
  z-index: 100;
}

.back-btn {
  width: 40px;
  height: 40px;
  background-color: rgba(240, 217, 181, 0.1);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 10px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.back-btn:active {
  background-color: rgba(240, 217, 181, 0.2);
  transform: scale(0.95);
}

.back-arrow {
  color: #F0D9B5;
  font-size: 20px;
  font-weight: bold;
}

.title {
  flex: 1;
  color: rgba(255, 255, 255, 0.5);
  font-size: 16px;
  font-weight: 600;
  text-align: center;
}

.search-section {
  padding: 20rpx;
}

.search-input {
  background-color: #4A4062;
  border-radius: 8rpx;
  padding: 20rpx;
  display: flex;
  align-items: center;
}

.search-icon {
  width: 32rpx;
  height: 32rpx;
  margin-right: 15rpx;
  opacity: 0.6;
}

.search-input input {
  color: #FFFFFF;
  font-size: 32rpx;
  background-color: transparent;
  border: none;
  flex: 1;
}

.search-input input::placeholder {
  color: #8F82A8;
  font-size: 32rpx;
}

.filter-tags {
  display: flex;
  padding: 0 20rpx 20rpx;
  gap: 20rpx;
  flex-wrap: wrap;
}

.filter-tag {
  flex: 1;
  background-color: #4A4062;
  color: #8F82A8;
  font-size: 32rpx;
  text-align: center;
  padding: 20rpx;
  border-radius: 8rpx;
  min-width: 150rpx;
}

.filter-tag.active {
  background-color: #F8E7D0;
  color: #3A3052;
}

.cash-list {
  padding: 0 20rpx 20rpx;
}

.cash-item {
  background-color: #4A4062;
  border-radius: 16rpx;
  padding: 25rpx;
  margin: 20rpx 0;
}

.cash-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20rpx;
}

.cash-info {
  display: flex;
  align-items: center;
  gap: 20rpx;
}

.cash-amount {
  color: #F0D9B5;
  font-size: 40rpx;
  font-weight: bold;
}

.cash-status {
  padding: 5rpx 15rpx;
  border-radius: 15rpx;
  font-size: 24rpx;
}

.status-processing {
  background-color: rgba(255, 193, 7, 0.2);
  color: #FFC107;
}

.status-success {
  background-color: rgba(76, 175, 80, 0.2);
  color: #4CAF50;
}

.status-failed {
  background-color: rgba(244, 67, 54, 0.2);
  color: #F44336;
}

.cash-time {
  color: #8F82A8;
  font-size: 28rpx;
}

.cash-details {
  border-top: 1rpx solid #52496D;
  padding-top: 20rpx;
}

.detail-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10rpx 0;
}

.detail-label {
  color: #8F82A8;
  font-size: 28rpx;
}

.detail-value {
  color: #FFFFFF;
  font-size: 28rpx;
}

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 100rpx 0;
}

.empty-image {
  width: 200rpx;
  height: 200rpx;
  margin-bottom: 30rpx;
  opacity: 0.5;
}

.empty-text {
  color: #8F82A8;
  font-size: 32rpx;
}

.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: 120rpx;
  background-color: #2A2240;
  display: flex;
  justify-content: space-around;
  align-items: center;
  border-top: 1rpx solid #3A3052;
}

.nav-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: #8F82A8;
}

.nav-item.active {
  color: #F0D9B5;
}

.nav-item image {
  width: 50rpx;
  height: 50rpx;
  margin-bottom: 10rpx;
}

.nav-item text {
  font-size: 24rpx;
}
</style>